package com.naiterui.ehp.bs.doctor.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IChargeService;
import com.naiterui.ehp.bs.doctor.service.IConsultSessionService;
import com.naiterui.ehp.bs.doctor.vo.ChargeVO;
import com.naiterui.ehp.bs.doctor.vo.ConsultCostVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 医生服务收费Controller
 *
 * @author gaoliangliang
 * @date 2021/1/26 上午8:45
 **/
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.SERVICE_CHARGE})
@Slf4j
@RestController
@RequestMapping("ad/charge")
@RequiredArgsConstructor
@Validated
public class AdChargeController {

    private final IChargeService chargeService;
    private final IConsultSessionService consultSessionService;

    @GetMapping("{type}")
    @ApiOperation(value = "获取医生指定服务收费信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "收费服务类型，1：图文咨询，2：视频问诊", required = true, dataType = "long", paramType = "path"),
    })
    public ResponseVO<ChargeVO> chargeInfoByType(
            @NotNull(message = "医生ID不能为空") Long doctorId,
            @NotNull(message = "收费服务类型不能为空") @PathVariable Integer type) {
        log.info("获取医生服务收费信息，参数为：doctorId={},type={}", doctorId, type);
        ChargeVO chargeInfo = this.chargeService.getChargeInfoByType(doctorId, type);
        return PublicService.returnResponseVO(chargeInfo);
    }

    @GetMapping
    @ApiOperation(value = "获取医生所有已开通服务收费信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query"),
    })
    public ResponseVO<List<ChargeVO>> chargeInfo(
            @NotNull(message = "医生ID不能为空") Long doctorId) {
        log.info("获取医生所有服务收费信息，参数为：doctorId={}", doctorId);
        List<ChargeVO> chargeInfos = this.chargeService.getChargeInfo(doctorId);
        return PublicService.returnResponseVO(chargeInfos);
    }

    @PostMapping
    @ApiOperation(value = "设置医生服务收费信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "收费服务类型，1：图文咨询,2:视频问诊", allowableValues = "1,2", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "charge", value = "收费金额（分）", required = true, dataType = "long", paramType = "query")
    })
    public ResponseVO<Void> saveCharge(
            @NotNull(message = "医生ID不能为空") Long doctorId,
            @NotNull(message = "收费服务类型不能为空") Integer type,
            @NotNull(message = "收费金额不能为空") Integer charge) throws BusinessException {
        log.info("设置医生服务收费信息，参数为：doctorId={},type={},charge={}", doctorId, type, charge);
        this.chargeService.saveChargeInfo(doctorId, type, charge);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    @GetMapping("/getCost")
    @ApiOperation(value = "获取咨询费用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "patientId", value = "患者id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "inquirerId", value = "就诊人id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "chargeType", value = "收费类型，1：图文咨询,2:视频问诊", allowableValues = "1,2", required = true, dataType = "int", paramType = "query")
    })
    public ResponseVO<ConsultCostVO> getCost(@NotNull(message = "医生ID不能为空") Long doctorId,
                                             @NotNull(message = "患者ID不能为空") Long patientId,
                                             @NotNull(message = "就诊人ID不能为空") Long inquirerId,
                                             @NotNull(message = "收费类型不能为空") Integer chargeType) throws BusinessException {
        ConsultCostVO consultCostVO = consultSessionService.findConsultCostByDoctorIdAndPatientIdAndInquirerId(doctorId, patientId, inquirerId, chargeType);
        return PublicService.returnResponseVO(consultCostVO);
    }
}
